package com.fanjia.flowable.flow.controller;

import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@RestController
@RequestMapping("/flowable")
public class FlowableController {

    @Autowired
    ProcessEngine processEngine;

    /**
     * 部署流程定义
     */
    @RequestMapping("/definition")
    public String deploymentFlowDefinition(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("flowable/leave_name.bpmn20.xml")
                .name("请假流程部署")
                .deploy();
        return "部署成功";
    }


    /**
     * 查询流程定义
     */
    @RequestMapping("/query/definition")
    public String getFlowDefinition(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();
        for (ProcessDefinition processDefinition : list){

            System.out.println("名字:"+processDefinition.getName());



        }
        return "成功";
    }




    /**
     * 启动流程实例
     */
    @RequestMapping("/process/instance")
    public String processInstance(){

        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("employee", "fanjia");
        variables.put("nrOfHolidays", "8");
        variables.put("description", "ill");
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("leave_key", variables);
        return "成功";
    }




    /**
     * 查看任务表
     */
    @RequestMapping("/query/task")
    public String queryTask(){
        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery().taskAssignee("张三").singleResult();
        Map<String, Object> processVariables = taskService.getVariables(task.getId());
        System.out.println("员工名字"+processVariables.get("employee"));
        System.out.println("员工请假天数"+processVariables.get("nrOfHolidays"));
        System.out.println("员工理由"+processVariables.get("description"));
        return "成功";
    }


    /**
     * 完成自己的任务
     */
    @RequestMapping("/complete/task")
    public String completeTask(){
        TaskService taskService = processEngine.getTaskService();
        String taskId = "5008";
        taskService.complete(taskId);
        return "成功";
    }




    /**
     * 查看自己的历史
     */
    @RequestMapping("/history/task")
    public String historyTask(){
        String name = "张三";
        HistoryService historyService = processEngine.getHistoryService();
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().taskAssignee(name).list();
        for (HistoricTaskInstance historicTaskInstance: list){

            System.out.println(historicTaskInstance.getName() + "=====" +new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(historicTaskInstance.getEndTime()));
        }
        return "成功";
    }



























}
